﻿using System.Buffers;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using eLibrary;

namespace eNets.CBK.Session;

abstract class ASession
{
    public ASession(Socket socket)
    {
        this.Id = StringIdGenerator.GetNextId();
        this.socket = socket;
        this.LocalEndPoint = socket.LocalEndPoint;
    }

    protected Socket socket;

    public string Id
    {
        get;
    }

    public abstract bool Active
    {
        get;
    }

    public EndPoint LocalEndPoint
    {
        get;
    }

    public EndPoint RemoteEndPoint
    {
        get;
        protected set;
    }

    public abstract ValueTask StartAsync();
    
    public virtual ValueTask CloseAsync()
    {
        try
        {
            this.socket.Shutdown(SocketShutdown.Both);
        }
        catch
        {
        }

        this.socket.Dispose();
        
        return new ValueTask();
    }
}